<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\Common;

class LongbingServiceNotice extends LongbingCurl
{
    protected $access_token;
    protected $appid;
    protected $appsecret;
    protected $uniacid = "7777";
    protected $config = [];
    public function __construct($uniacid = "7777")
    {
        $this->uniacid = $uniacid;
        $this->config = $this->getConfig($this->uniacid);
        $this->appid = $this->getAppid();
        $this->appsecret = $this->getAppsecret();
    }
    protected function getAppid()
    {
        if(isset($this->config["appid"])) {
            return $this->config["appid"];
        }
        return NULL;
    }
    protected function getAppsecret()
    {
        if(isset($this->config["app_secret"])) {
            return $this->config["app_secret"];
        }
        return NULL;
    }
    public function getConfig($uniacid)
    {
        $key = "longbing_card_app_config_" . $uniacid;
        $config = getCache($key, $uniacid);
        if(!empty($config)) {
            return $config;
        }
        $config_model = new \app\admin\model\AppConfig();
        $config = $config_model->getConfigByUniacid($uniacid);
        if(!empty($config)) {
            setCache($key, $config, 3600, $uniacid);
        }
        return $config;
    }
    public function checkConfig()
    {
        $result = true;
        if(empty($this->uniacid) || empty($this->appid) || empty($this->appsecret)) {
            $result = false;
        }
        return $result;
    }
    public function lbSingleGetAccessToken()
    {
        $setting = new \longbingcore\wxcore\WxSetting($this->uniacid);
        $token = $setting->lbSingleGetAccessToken();
        return $token;
    }
    public function lbSingleGetAccessTokenNew()
    {
        if(!$this->checkConfig()) {
            return false;
        }
        $appid = $this->appid;
        $appsecret = $this->appsecret;
        $key = "longbing_app_access_" . md5($appid);
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
        $data = file_get_contents($url);
        $data = json_decode($data, true);
        if(!isset($data["access_token"])) {
            return false;
        }
        $access_data["access_token"] = $access_token = $data["access_token"];
        $access_data["ac_time"] = time() + 7200;
        $result = setCache($key, $access_data, 7200, $this->uniacid);
        return $access_token;
    }
    public function sendServiceNoticeToStaff($count_id, $to_user = NULL, $extra_data = [])
    {
        $count_model = new model\LongbingCardCount();
        $count = $count_model->getCount(["id" => $count_id]);
        if(empty($count) || empty($count["user_id"]) || empty($count["to_uid"])) {
            return false;
        }
        if($count["user_id"] == $count["to_uid"]) {
            $to_user = false;
        }
        $check_where = ["user_id" => $count["user_id"], "to_uid" => $count["to_uid"], "type" => $count["type"], "uniacid" => $count["uniacid"], "target" => $count["target"], "sign" => $count["sign"]];
        if(($count["type"] == 18 || $count["type"] == 19 || $count["type"] == 20) && $count["sign"] == "view") {
            unset($check_where["target"]);
        }
        $count_num = $count_model->getCountNum($check_where);
        if(empty($count_num)) {
            $count_num = 1;
        }
        $client_info_model = new model\LongbingClientInfo();
        $client_info = $client_info_model->getClientInfo(["user_id" => $count["user_id"], "staff_id" => $count["to_uid"]]);
        if(!empty($client_info) && isset($client_info["is_mask"]) && !empty($client_info["is_make"])) {
            return false;
        }
        $send_body = $this->lbSingleGetSendBody($count, 0, $extra_data);
        if(empty($send_body)) {
            return false;
        }
        $user = longbingGetUser($count["user_id"], $this->uniacid);
        $staff = longbingGetUser($count["to_uid"], $this->uniacid);
        if(!empty($to_user)) {
            $this->sendServiceNoticeToUser($count, $count_num, $send_body, $user);
        }
        switch ($this->config["notice_switch"]) {
            case 1:
                if(!isset($staff["openid"])) {
                } else {
                    $this->sendWxOAServiceNotice($staff["openid"], $user["nickName"], $send_body, $count["update_time"]);
                }
                break;
            case 2:
                $this->sendWxEnterpriseServiceNotice($count["to_uid"], $user["nickName"], $send_body, $count);
                break;
            case 3:
                $from_id = $this->lbSingleGetFormId($count["to_uid"]);
                if(!isset($staff["openid"]) || empty($from_id)) {
                } else {
                    $this->sendWxService($staff["openid"], $from_id, $user["nickName"], $send_body, $count["update_time"]);
                }
                break;
            case 4:
                $this->sendCompanyWxEnterpriseServiceNotice($count["to_uid"], $user["nickName"], $send_body);
                break;
            default:
                $from_id = $this->lbSingleGetFormId($count["to_uid"]);
                if(!isset($staff["openid"]) || empty($from_id)) {
                } else {
                    $this->sendWxService($staff["openid"], $from_id, $user["nickName"], $send_body, $count["update_time"]);
                }
        }
    }
    public function sendCompanyWxEnterpriseServiceNotice($to_uid, $nick_name, $send_body)
    {
        $wx_msg = new \longbingcore\wxcore\WxMsg($this->uniacid);
        $user_info_model = new model\LongbingUserInfo();
        $user_info = $user_info_model->getUser(["fans_id" => $to_uid]);
        $touser = NULL;
        if(isset($user_info["ww_account"])) {
            $touser = $user_info["ww_account"];
        }
        if(empty($touser)) {
            return false;
        }
        $content = [["key" => "访问用户", "value" => $nick_name], ["key" => "内容", "value" => $send_body]];
        $res = $wx_msg->WxMsg($touser, "访问通知", "pages/admin/radar/radar", date("Y-m-d H:i:s", time()), $content);
        return $res;
    }
    public function sendCompanyWxEnterpriseServiceNoticeIm($to_uid, $nick_name, $send_body)
    {
        $wx_msg = new \longbingcore\wxcore\WxMsg($this->uniacid);
        $user_info_model = new model\LongbingUserInfo();
        $user_info = $user_info_model->getUser(["fans_id" => $to_uid]);
        $touser = NULL;
        if(isset($user_info["ww_account"])) {
            $touser = $user_info["ww_account"];
        }
        if(empty($touser)) {
            return false;
        }
        $content = [["key" => "访问用户", "value" => $nick_name], ["key" => "内容", "value" => $send_body]];
        $res = $wx_msg->WxMsg($touser, "聊天通知", "pages/admin/chat/chat", date("Y-m-d H:i:s", time()), $content);
        return $res;
    }
    public function sendImMessageServiceNoticeToStaff($staff_id, $message, $name = "")
    {
        if(service\LongbingUserInfoService::isStraffByUserId($staff_id)) {
            $openid = service\LongbingUserService::getUserOpenId($staff_id);
            $name = empty($name) ? service\LongbingUserInfoService::getNameByUserId($staff_id) : $name;
            $time = \longbingcore\tools\LongbingTime::getChinaNowTime();
            switch ($this->config["notice_switch"]) {
                case 1:
                    $this->sendWxOAServiceNotice($openid, $name, $message, $time);
                    break;
                case 2:
                    $count = ["sign" => "view", "type" => 10000];
                    $this->sendWxEnterpriseServiceNotice($staff_id, $name, $message, $count);
                    break;
                case 4:
                    $this->sendCompanyWxEnterpriseServiceNoticeIm($staff_id, $name, $message);
                    break;
            }
        }
    }
    public function sendServiceNoticeToUser($count, $count_num, $send_body, $user)
    {
        $uniacid = $this->uniacid;
        if(empty($count_num)) {
            $count_num = 1;
        }
        if(empty($send_body)) {
            return false;
        }
        if(empty($count) || !isset($count["user_id"])) {
            return false;
        }
        if(!isset($user["openid"]) || !isset($user["nickName"])) {
            return false;
        }
        $openid = $user["openid"];
        if(!isset($count["create_time"]) || empty($count["create_time"])) {
            $count["create_time"] = time();
        }
        $from_id = $this->lbSingleGetFormId($count["user_id"]);
        if(empty($from_id)) {
            return false;
        }
        return $this->sendWxService($openid, $from_id, $user["nickName"], $send_body, $count["create_time"]);
    }
    public function sendMessageServiceNotice($message)
    {
        $uniacid = $this->uniacid;
        if(empty($message) || !isset($message["content"])) {
            return false;
        }
        $user = longbingGetUser($message["user_id"], $this->uniacid);
        if(empty($user) || !isset($user["nickName"])) {
            return false;
        }
        $to_user = longbingGetUser($message["to_user_id"], $this->uniacid);
        if(!isset($to_user["openid"]) || !isset($to_user["id"])) {
            return false;
        }
        $openid = $to_user["openid"];
        if(!isset($message["create_time"]) || empty($message["create_time"])) {
            $message["create_time"] = time();
        }
        $from_id = $this->lbSingleGetFormId($to_user["id"]);
        if(empty($from_id)) {
            return false;
        }
        $page = "pages/user/home";
        if(isset($to_user["is_staff"]) && !empty($to_user["is_staff"])) {
            $page = "pages/admin/chat/chat";
        }
        return $this->sendWxService($openid, $from_id, $user["nickName"], $message["content"], $message["create_time"], $page);
    }
    public function sendWxService($openid, $form, $nickName, $send_body, $time = NULL, $page_data = NULL)
    {
        $access_token = $this->lbSingleGetAccessToken();
        if(empty($access_token)) {
            return false;
        }
        if(!isset($this->config["mini_template_id"])) {
            return false;
        }
        $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" . $access_token;
        $page = "pages/admin/radar/radar";
        if(!empty($page_data)) {
            $page = $page_data;
        }
        if(empty($time) || !is_int($time)) {
            $time = time();
        }
        $date = date("Y-m-d H:i", $time);
        $postData = ["touser" => $openid, "template_id" => $this->config["mini_template_id"], "page" => $page, "form_id" => $form, "data" => ["keyword1" => ["value" => $nickName], "keyword2" => ["value" => $send_body], "keyword3" => ["value" => $date]]];
        $postData = json_encode($postData, JSON_UNESCAPED_UNICODE);
        $response = $this->curlPost($url, $postData);
        return $response;
    }
    public function addTmpl($data)
    {
        $access_token = $this->lbSingleGetAccessToken();
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token=" . $access_token;
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        $res = $this->curlPost($url, $data);
        return $res;
    }
    public function sendWxOAServiceNotice($openid, $nickName, $send_body, $time = NULL)
    {
        $wx_appid = NULL;
        $wx_tplid = NULL;
        if(isset($this->config["wx_appid"])) {
            $wx_appid = $this->config["wx_appid"];
        }
        if(isset($this->config["wx_tplid"])) {
            $wx_tplid = $this->config["wx_tplid"];
        }
        if(empty($wx_appid) || empty($wx_tplid)) {
            return false;
        }
        $access_token = $this->lbSingleGetAccessToken();
        if(empty($access_token)) {
            return false;
        }
        $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" . $access_token;
        if(empty($time) || !is_int($time)) {
            $time = time();
        }
        $date = date("Y-m-d H:i", $time);
        $page = "pages/admin/radar/radar";
        $data = ["touser" => $openid, "mp_template_msg" => ["appid" => $wx_appid, "url" => "http://weixin.qq.com/download", "template_id" => $wx_tplid, "miniprogram" => ["appid" => $this->appid, "pagepath" => $page], "data" => ["first" => ["value" => "", "color" => "#c27ba0"], "keyword1" => ["value" => $nickName, "color" => "#93c47d"], "keyword2" => ["value" => $send_body, "color" => "#0000ff"], "remark" => ["value" => $date, "color" => "#45818e"]]]];
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        $res = $this->curlPost($url, $data);
    }
    public function sendWxEnterpriseServiceNotice($to_uid, $nick_name, $send_body, $count_info)
    {
        $app_id = NULL;
        $appsecret = NULL;
        $agentid = NULL;
        if(isset($this->config["corpid"])) {
            $app_id = $this->config["corpid"];
        }
        if(isset($this->config["corpsecret"])) {
            $appsecret = $this->config["corpsecret"];
        }
        if(isset($this->config["agentid"])) {
            $agentid = $this->config["agentid"];
        }
        if(empty($app_id) || empty($appsecret) || empty($agentid)) {
            return false;
        }
        $user_info_model = new model\LongbingUserInfo();
        $user_info = $user_info_model->getUser(["fans_id" => $to_uid]);
        $touser = NULL;
        if(isset($user_info["ww_account"])) {
            $touser = $user_info["ww_account"];
        }
        if(empty($touser)) {
            return false;
        }
        $data = ["touser" => $touser, "msgtype" => "text", "agentid" => $agentid, "text" => ["content" => $nick_name . "," . $send_body]];
        if($count_info["sign"] == "view" && $count_info["type"]) {
            if(in_array($count_info["type"], [662, 663, 664, 665, 666])) {
                $table_name = "longbing_card_shortvideo";
            } elseif(in_array($count_info["type"], [1, 19, 21, 22, 23, 24])) {
                $table_name = "longbing_card_goods";
            }
            if(!empty($table_name)) {
                $info = $this->longbingGetRow($table_name, ["id" => $count_info["target"]]);
            }
            if(!empty($info["cover"])) {
                $info = transImagesOne($info, ["cover"]);
                $cover = $info["cover"];
            }
            $data = ["touser" => $touser, "msgtype" => "news", "agentid" => $agentid, "news" => ["articles" => [["title" => $nick_name, "description" => $send_body]]]];
        }
        if(!empty($cover)) {
            $data["news"]["articles"][0]["url"] = $cover;
            $data["news"]["articles"][0]["picurl"] = $cover;
        }
        $work = new extend\wxWork\work($app_id, $appsecret);
        $result = $work->send($data);
    }
    public function lbSingleAutoDelFromId()
    {
        $from_id_model = new model\LongbingCardFromId();
        $from_id_model->autoDelFromId();
    }
    public function lbSingleGetFormId($to_uid)
    {
        $beginTime = mktime(0, 0, 0, date("m"), date("d") - 6, date("Y"));
        $this->lbSingleAutoDelFromId();
        $from_id_model = new model\LongbingCardFromId();
        $from_id = $from_id_model->getFromId(["user_id" => $to_uid]);
        if(empty($from_id)) {
            return false;
        }
        if($from_id["create_time"] < $beginTime) {
            $from_id_model->delFromId(["id" => $from_id["id"]]);
            return $this->lbSingleGetFormId($to_uid);
        }
        $from_id_model->delFromId(["id" => $from_id["id"]]);
        return $from_id["formId"];
    }
    public function longbingGetRow($table_name, $filter)
    {
        if(empty($table_name) || empty($filter) || !is_array($filter)) {
            return false;
        }
        $common_model = new model\LongbingCardCommonModel();
        return $common_model->getRows($table_name, $filter);
    }
    public function lbSingleGetSendBody($count_info = [], $count_id = 0, $extra_data = [])
    {
        $uniacid = $this->uniacid;
        $tabbar_model = new model\LongbingTabbar();
        $tabbar = $tabbar_model->getTabbar(["uniacid" => $uniacid]);
        if(empty($count_info) && $count_id == 0) {
            return false;
        }
        if(empty($count_info) && $count_id != 0) {
            $count_model = new model\LongbingCardCount();
            $count_info = $count_model->getCount(["id" => $count_id]);
            if(!$count_info) {
                return false;
            }
        }
        $datas = lbHandelRadarMsg([$count_info]);
        $body = "";
        if(!empty($datas[0]["radar_arr"])) {
            foreach ($datas[0]["radar_arr"] as $value) {
                $body .= $value["title"];
            }
        }
        return $body;
    }
}

?>